XKalendar = new Object();


XKalendarModel = function() {
	this.rowHeaders = new Array();
	this.columns = new Array();
	this.entries = new Array();
	
	this.showColumnHeaders = true;
	this.showRowHeaders = true;
}

XKalendarModel.prototype.setView = function(view) {
	this.view = view;
}

XKalendarModel.prototype.getView = function() {
	return this.view;
}

XKalendarModel.prototype.addRowHeader = function(rowHeader) {
	this.rowHeaders[this.rowHeaders.length] = rowHeader;
}

XKalendarModel.prototype.getRowHeader = function(index) {
	return this.rowHeaders[index];
	
}

XKalendarModel.prototype.setShowColumnHeaders = function(showColumnHeaders) {
	this.showColumnHeaders = showColumnHeaders;
}

XKalendarModel.prototype.getShowColumnHeaders = function() {
	return this.showColumnHeaders;
}

XKalendarModel.prototype.setShowRowHeaders = function(showRowHeaders) {
	this.showRowHeaders = showRowHeaders;
}

XKalendarModel.prototype.getShowRowHeaders = function() {
	return this.showRowHeaders;
}

XKalendarModel.prototype.setMode = function(mode) {
	this.mode = mode;
}

XKalendarModel.prototype.getMode = function() {
	return this.mode;
}

XKalendarModel.prototype.setDate1 = function(date1) {
	this.date1 = date1;
}

XKalendarModel.prototype.getDate1 = function() {
	return this.date1;
}

XKalendarModel.prototype.setDate2 = function(date2) {
	this.date2 = date2;
}

XKalendarModel.prototype.getDate2 = function() {
	return this.date2;
}

XKalendarModel.prototype.getNumberOfRowHeaders = function() {
	return this.rowHeaders.length;
}

XKalendarModel.prototype.clearRowHeaders = function() {
	this.rowHeaders.length = 0;
}

XKalendarModel.prototype.createRowHeader = function() {
	var rowHeaderModel = new XKalendarRowHeaderModel();
	this.addRowHeader(rowHeaderModel);
	return rowHeaderModel;
}

XKalendarModel.prototype.addColumn = function(column) {
	this.columns[this.columns.length] = column;
}

XKalendarModel.prototype.clearColumns = function() {
	this.columns.length = 0;
}

XKalendarModel.prototype.getColumn = function(index) {
	return this.columns[index];
}

XKalendarModel.prototype.getNumberOfColumns = function() {
	return this.columns.length;
}

XKalendarModel.prototype.createColumn = function() {
	var columnModel = new XKalendarColumnModel();
	this.addColumn(columnModel);
	return columnModel;
}

XKalendarModel.prototype.addEntry = function(entry) {
	this.entries[this.entries.length] = entry;
}

XKalendarModel.prototype.createEntry = function() {
	var entryModel = new XKalendarEntryModel();
	this.addEntry(entryModel);
	return entryModel;
}

XKalendarModel.prototype.clearEntries = function() {
	this.entries.length = 0;
}

XKalendarModel.prototype.getEntry = function(index) {
	return this.entries[index];
}

XKalendarModel.prototype.getNumberOfEntries = function() {
	return this.entries.length;
}

XKalendarModel.prototype.reset = function() {
	this.clearColumns();
	this.clearRowHeaders();
}

XKalendarModel.prototype.configureToDayMode = function(date) {
	this.reset();
	this.setMode("day");
	this.setDate1(date);
	this.prepareToPopulate();
	var calendarView = this.getView();
	if(calendarView!=null) {
		XKalendar.populateView(calendarView, this);
	}
}

XKalendarModel.prototype.configureToWeekMode = function(date1, date2) {
	this.reset();
	this.setMode("week");
	this.setDate1(date1);
	this.setDate2(date2);
	this.prepareToPopulate();
	var calendarView = this.getView();
	if(calendarView!=null) {
		XKalendar.populateView(calendarView, this);
	}
}

XKalendarModel.prototype.configureToListMode = function() {
	this.reset();
	this.setMode("list");
	this.prepareToPopulate();
	var calendarView = this.getView();
	if(calendarView!=null) {
		XKalendar.populateView(calendarView, this);
	}
}

XKalendarModel.prototype.prepareToPopulate = function() {
	XKalendar.createRowsHeaders(this);
	
	if(this.getMode() == "day") {
		this.setShowColumnHeaders(true);
		this.setShowRowHeaders(true);
		XKalendar.createColumn(this, this.getDate1());		
	} else if(this.getMode() == "week") {
		this.setShowColumnHeaders(true);
		this.setShowRowHeaders(true);
		var columnsAdded = 0;
		var date = new Date(this.getDate1());
		var found = false;
		while(!found && columnsAdded<100) {
			if(date>this.getDate2()) {
				found = true;
			} else {
				XKalendar.createColumn(this, date);
				columnsAdded++;
			}
			date.setDate(date.getDate() + 1);
		}		
	} else if(this.getMode() == "list") {
		this.setShowColumnHeaders(false);
		this.setShowRowHeaders(false);
	}
}

XKalendarModel.prototype.switchToDayMode = function() {
	var date = new Date();	
	this.configureToDayMode(date);
}

XKalendarModel.prototype.switchToWeekMode = function() {
	var date1 = new Date();
	while(date1.getDay()!=0) {
		date1.setDate(date1.getDate() - 1);
	}
	var date2 = new Date(date1);
	date2.setDate(date2.getDate() + 6);	
	this.configureToWeekMode(date1, date2);
}

XKalendarModel.prototype.switchToMonthMode = function() {
}

XKalendarModel.prototype.switchToYearMode = function() {
}

XKalendarModel.prototype.switchToListMode = function() {
	this.configureToListMode();
}

XKalendarModel.prototype.jumpBackward = function() {
	if(this.getMode()=="day") {
		var newDate = new Date(this.getDate1());
		newDate.setDate(newDate.getDate() - 1);
		this.configureToDayMode(newDate);
	} else if (this.getMode()=="week") {
		var newDate1 = new Date(this.getDate1());
		newDate1.setDate(newDate1.getDate() - 7);
		var newDate2 = new Date(this.getDate2());
		newDate2.setDate(newDate2.getDate() - 7);
		this.configureToWeekMode(newDate1, newDate2);
	}
}

XKalendarModel.prototype.jumpForward = function() {
	if(this.getMode()=="day") {
		var newDate = new Date(this.getDate1());
		newDate.setDate(newDate.getDate() + 1);
		this.configureToDayMode(newDate);
	} else if (this.getMode()=="week") {
		var newDate1 = new Date(this.getDate1());
		newDate1.setDate(newDate1.getDate() + 7);
		var newDate2 = new Date(this.getDate2());
		newDate2.setDate(newDate2.getDate() + 7);
		this.configureToWeekMode(newDate1, newDate2);
	}
}

XKalendarModel.prototype.jumpToday = function() {
	if(this.getMode()=="day") {
		this.switchToDayMode();
	} else if (this.getMode()=="week") {
		this.switchToWeekMode();
	}
}

XKalendarColumnModel = function() {	
	this.cells = new Array();
}

XKalendarColumnModel.prototype.addCell = function(cell) {
	this.cells[this.cells.length] = cell;
}

XKalendarColumnModel.prototype.getCell = function(index) {
	return this.cells[index];
}

XKalendarColumnModel.prototype.getNumberOfCells = function() {
	return this.cells.length;
}

XKalendarColumnModel.prototype.createCell = function() {
	var cellModel = new XKalendarCellModel();
	this.addCell(cellModel);
	return cellModel;
}

XKalendarColumnModel.prototype.setDate = function(date) {
	this.date = date;
}

XKalendarColumnModel.prototype.getDate = function() {
	return this.date;
}

XKalendarRowHeaderModel = function() {
}

XKalendarRowHeaderModel.prototype.setText = function(text) {
	this.text = text;
}

XKalendarRowHeaderModel.prototype.getText = function() {
	return this.text;
}

XKalendarColumnHeaderModel = function() {
}

XKalendarColumnHeaderModel.prototype.setText = function(text) {
	this.text = text;
}

XKalendarColumnHeaderModel.prototype.getText = function() {
	return this.text;
}

XKalendarColumnFooterModel = function() {
}

XKalendarColumnBodyModel = function() {
}

XKalendarCellModel = function() {
	this.header = new XKalendarCellHeaderModel();
}

XKalendarCellModel.prototype.getHeader = function() {
	return this.header;
}

XKalendarCellHeaderModel = function() {
}

XKalendarCellHeaderModel.prototype.setText = function(text) {
	this.text = text;
}

XKalendarCellHeaderModel.prototype.getText = function() {
	return this.text;
}

XKalendarCellBodyModel = function() {
}

XKalendarEntryModel = function() {
}

XKalendarEntryModel.prototype.setView = function(view) {
	this.view = view;
}

XKalendarEntryModel.prototype.getView = function() {
	return this.view;
}

XKalendarEntryModel.prototype.setTitle = function(title) {
	this.title = title;
}

XKalendarEntryModel.prototype.getTitle = function() {
	return this.title;
}

XKalendarEntryModel.prototype.setStartDate = function(startDate) {
	this.startDate = startDate;
}

XKalendarEntryModel.prototype.getStartDate = function() {
	return this.startDate;
}

XKalendarEntryModel.prototype.setFinishDate = function(finishDate) {
	this.finishDate = finishDate;
}

XKalendarEntryModel.prototype.getFinishDate = function() {
	return this.finishDate;
}

XKalendar.createView = function(calendarModel) {
	var calendarView = new WindView();
	calendarModel.setView(calendarView);

	XKalendar.populateView(calendarView, calendarModel);	

	return calendarView;
}

XKalendar.populateView = function(calendarView, calendarModel) {
	calendarView.clearViews();
	calendarView.setCssClass("XKalendar");

	var calendarTableView = new WindView();
	calendarTableView.setCssClass("XKalendarTable");
	
	calendarView.addView(calendarTableView);

	
	var y = 0;
	var totalHeight = 45; 
	var totalWidth = 800;
	var borderBottom = 1;

	var calendarHeaderView = XKalendar.createHeaderView(calendarModel, y, totalWidth, totalHeight);
	calendarTableView.addView(calendarHeaderView);

	y += totalHeight + borderBottom;

	totalHeight = 22; 
	firstWidth = 50; 

	if(calendarModel.getShowColumnHeaders()) {
		
		var calendarColumnHeadersView = XKalendar.createColumnHeaders(calendarModel, y, totalWidth, firstWidth, totalHeight);
		calendarTableView.addView(calendarColumnHeadersView);

		y += totalHeight;
	}
	
	totalHeight = (totalHeight*calendarModel.getNumberOfRowHeaders());;
	
	XKalendar.createRowsViews(calendarModel, calendarTableView, y, totalWidth, firstWidth, totalHeight);
	
	calendarTableView.setHeight(y + totalHeight - borderBottom);
	calendarTableView.setWidth(totalWidth);
	
	
	//var calendarFooterView = new WindView();
	//calendarFooterView.setCssClass("XKalendarFooter");
	//calendarView.addView(calendarFooterView);

	return calendarView;
}

XKalendar.createHeaderView = function(calendarModel, y, totalWidth, totalHeight) {
	var calendarHeaderView = new WindView();
	calendarHeaderView.setCssClass("XKalendarHeader");
	calendarHeaderView.setY(y);
	calendarHeaderView.setWidth(totalWidth);
	calendarHeaderView.setHeight(totalHeight);
	
	var calendarHeaderContentView = new WindView();
	calendarHeaderView.addView(calendarHeaderContentView);
	
	var layoutView = WindLayout.createView();
	layoutView.setId("XKalendarHeaderLayout")
	calendarHeaderContentView.addView(layoutView);
	
	var rowView = layoutView.createRowView();

	{
		var cellView = rowView.createCellView();
		
		var calendarHeaderControlsView = new WindView();
		calendarHeaderControlsView.setCssClass("XKalendarHeaderControls");
		
		var segmentedModel = new WindSegmentedModel();
		{
			var buttonModel = segmentedModel.createButton();
			buttonModel.setTitle("Today");
			buttonModel.onClick = function() {
				calendarModel.jumpToday();	
			}
		}
		{
			var buttonModel = segmentedModel.createButton();
			buttonModel.setTitle("<");
			buttonModel.onClick = function() {
				calendarModel.jumpBackward();	
			}
		}
		{
			var buttonModel = segmentedModel.createButton();
			buttonModel.setTitle(">");
			buttonModel.onClick = function() {
				calendarModel.jumpForward();	
			}
		}
		var segmentedView = WindSegmented.createView(segmentedModel);
		calendarHeaderControlsView.addView(segmentedView);
		cellView.addView(calendarHeaderControlsView);	
	}
	
	{
		var cellView = rowView.createCellView();
		
		var calendarHeaderTitleView = new WindView();
		calendarHeaderTitleView.setCssClass("XKalendarHeaderTitle");
		
		var title = "";
		if(calendarModel.getMode() == "week") {
			title = XKalendar.getWeekName(calendarModel.getDate1(), calendarModel.getDate2(), true, true);
		} else if (calendarModel.getMode() == "day") {
			title = XKalendar.getDateName(calendarModel.getDate1(), true, true);
		}
		
		calendarHeaderTitleView.setText(title);
		
		
		cellView.addView(calendarHeaderTitleView);	
	}


	{
		var cellView = rowView.createCellView();
		
		var calendarHeaderControlsView = new WindView();
		calendarHeaderControlsView.setCssClass("XKalendarHeaderControls");
		
		var segmentedModel = new WindSegmentedModel();
		{
			var buttonModel = segmentedModel.createButton();
			buttonModel.setTitle("Day");
			if(calendarModel.getMode()=="day") {
				buttonModel.setActive(true);
			} else {
				buttonModel.onClick = function() {
					calendarModel.switchToDayMode();	
				}
			}
		}
		{
			var buttonModel = segmentedModel.createButton();
			buttonModel.setTitle("Week");
			if(calendarModel.getMode()=="week") {
				buttonModel.setActive(true);
			} else {
				buttonModel.onClick = function() {
					calendarModel.switchToWeekMode();	
				}
			}
		}
		{
			var buttonModel = segmentedModel.createButton();
			buttonModel.setTitle("Month");
			if(calendarModel.getMode()=="month") {
				buttonModel.setActive(true);
			} else {
				buttonModel.onClick = function() {
					calendarModel.switchToMonthMode();	
				}
			}
		}
		{
			var buttonModel = segmentedModel.createButton();
			buttonModel.setTitle("Year");
			if(calendarModel.getMode()=="year") {
				buttonModel.setActive(true);
			} else {
				buttonModel.onClick = function() {
					calendarModel.switchToYearMode();	
				}
			}
		}
		{
			var buttonModel = segmentedModel.createButton();
			buttonModel.setTitle("List");
			if(calendarModel.getMode()=="list") {
				buttonModel.setActive(true);
			} else {
				buttonModel.onClick = function() {
					calendarModel.switchToListMode();	
				}
			}
		}
		var segmentedView = WindSegmented.createView(segmentedModel);
		calendarHeaderControlsView.addView(segmentedView);
		cellView.addView(calendarHeaderControlsView);	
	}


	return calendarHeaderView;
}

XKalendar.createColumnHeaders = function(calendarModel, y, totalWidth, firstWidth, height) {
	var columnHeadersView = new WindView();
	columnHeadersView.setCssClass("XKalendarColumnHeaders");
	columnHeadersView.setY(y);
	columnHeadersView.setWidth(totalWidth);
	columnHeadersView.setHeight(height);
	
	var x = 0;
	var borderWidth = 1;

	var cellMarginTop= 4;
	var cellMarginBottom = 4;
	var cellMarginLeft= 4;
	var cellMarginRight = 4;
	var cellBorderBottom = 1;
	var cellBorderRight = 1;
	
	{
		var columnHeaderView = new WindView();
		columnHeaderView.setCssClass("XKalendarColumnHeader");
		columnHeaderView.setText("&nbsp;");
		
		columnHeaderView.setX(x);
		columnHeaderView.setY(0);
		columnHeaderView.setWidth(firstWidth - cellBorderRight);
		columnHeaderView.setHeight(height - cellMarginTop - cellMarginBottom - cellBorderBottom);
		x += firstWidth;
		
		columnHeadersView.addView(columnHeaderView);
	}

	var witdh = ((totalWidth-firstWidth + cellBorderRight)/calendarModel.getNumberOfColumns());
	
	for(var i=0; i<calendarModel.getNumberOfColumns(); i++) {
		var columnModel = calendarModel.getColumn(i);
		var columnHeaderView = new WindView();
		
		if(XKalendar.isToday(columnModel.getDate())) {
			columnHeaderView.setCssClass("XKalendarColumnHeader XKalendarColumnHeaderToday");
		} else {
			columnHeaderView.setCssClass("XKalendarColumnHeader");
		}
		
		columnHeaderView.setX(x);
		columnHeaderView.setY(0);
		columnHeaderView.setWidth(witdh - cellBorderRight);
		columnHeaderView.setHeight(height - cellMarginTop - cellMarginBottom  - cellBorderBottom);
		x += columnHeaderView.getWidth();
		x += borderWidth;
		
		columnHeaderView.setText(XKalendar.getDateName(columnModel.getDate()));
		
		columnHeadersView.addView(columnHeaderView);
	}
	
	return columnHeadersView;
}


XKalendar.createRowsViews = function(calendarModel, calendarTableView, y, totalWidth, firstWidth, totalHeight) {

	var rowsView = new WindView();
	rowsView.setCssClass("XKalendarRows");
	rowsView.setY(y);
	rowsView.setHeight(totalHeight);
	rowsView.setWidth(totalWidth);

	var borderWidth = 1;
	
	var cellMarginTop= 4;
	var cellMarginBottom = 4;
	var cellMarginLeft= 4;
	var cellMarginRight = 4;
	var cellBorderBottom = 1;
	var cellBorderRight = 1;
	
	var rowY = 0;
	
	var height = totalHeight/calendarModel.getNumberOfRowHeaders();
	var witdh = ((totalWidth - firstWidth + cellBorderRight)/calendarModel.getNumberOfColumns());

	for(var i=0; i<calendarModel.getNumberOfRowHeaders(); i++) {
		var rowView = new WindView();
		rowView.setCssClass("XKalendarRow");
		rowView.setY(rowY);
		rowView.setHeight(height);
		rowView.setWidth(totalWidth);

		rowY += height;
		
		var x = 0;

		if(calendarModel.getShowRowHeaders()) {
			var rowHeaderModel = calendarModel.getRowHeader(i);
			var rowHeaderView = new WindView();
			rowHeaderView.setCssClass("XKalendarRowHeader");
			rowHeaderView.setText(rowHeaderModel.getText());
			rowHeaderView.setX(x);
			rowHeaderView.setHeight(height - cellMarginLeft - cellMarginRight - cellBorderBottom);
			rowHeaderView.setWidth(firstWidth - cellMarginTop - cellMarginBottom - cellBorderRight);
			x += firstWidth;
			rowView.addView(rowHeaderView);
		}
		
		for(var j=0; j<calendarModel.getNumberOfColumns(); j++) {
			var columnModel = calendarModel.getColumn(j);
			var cellModel = columnModel.getCell(i);
			var cellView = XKalendar.createCellView(calendarModel, calendarTableView, columnModel, cellModel);
			cellView.setX(x);
			cellView.setWidth(witdh - cellMarginLeft - cellMarginRight - cellBorderRight);
			cellView.setHeight(height - cellMarginTop - cellMarginBottom - cellBorderBottom);
			x += witdh;
			rowView.addView(cellView);			
		}
		
		rowsView.addView(rowView);
	}

	XKalendar.createEntriesViews(calendarModel, rowsView);
	
	calendarTableView.addView(rowsView);
}

XKalendar.createCellView = function(calendarModel, calendarTableView, columnModel, cellModel) {
	var cellView = new WindView();
	
	var text = cellModel.getHeader().getText();
	if(text==null || text.length<=0) {
		text = "&nbsp;";
	}
	cellView.setText(text);
	
	cellView.setCssClass("XKalendarCell");
	
	if(XKalendar.isToday(columnModel.getDate())) {
		cellView.setCssClass(cellView.getCssClass() + " XKalendarCellToday");
	}

	cellView.onMouseDoubleClick = function(e) {
		XKalendar.entityCreationMouseDoubleClick(cellModel, cellView, calendarModel, calendarTableView.getParentView());
	}
	
	/*
	cellView.createHtmlElement = function() {
		var htmlElement = document.createElement("td");
		return htmlElement;
	}
	*/
	
	return cellView;
}


XKalendar.createModelForToday = function() {
	return XKalendar.createModelForDay(new Date());
}

XKalendar.isToday = function(date) {
	var isToday = true;
	
	var today = new Date();
	
	if(date.getFullYear() != today.getFullYear()) {
		isToday = false;
	} else {
		if(date.getMonth() != today.getMonth()) {
			isToday = false;
		} else {
			if(date.getDate() != today.getDate()) {
				isToday = false;
			}
		}
	}
	
	return isToday;
}

XKalendar.getWeekDayName = function(day) {
	var dayName = "";
	if(day == 0) {
		dayName = "sunday";
	} else if (day==1) {
		dayName = "monday";
	} else if (day==2) {
		dayName = "tuesday";
	} else if (day==3) {
		dayName = "wednesday";
	} else if (day==4) {
		dayName = "thursday";
	} else if (day==5) {
		dayName = "friday";
	} else if (day==6) {
		dayName = "saturday";
	}
	return dayName;
}

XKalendar.getMonthName = function(month) {
	var monthName = "";
	if(month==0) {
		monthName = "january";
	} else if (month==1) {
		monthName = "february";
	} else if (month==2) {
		monthName = "march";
	} else if (month==3) {
		monthName = "april";
	} else if (month==4) {
		monthName = "may";
	} else if (month==5) {
		monthName = "june";
	} else if (month==6) {
		monthName = "july";
	} else if (month==7) {
		monthName = "august";
	} else if (month==8) {
		monthName = "september";
	} else if (month==9) {
		monthName = "october";
	} else if (month==10) {
		monthName = "november";
	} else if (month==11) {
		monthName = "december";
	} 
	return monthName;
}

XKalendar.getDateName = function(date, withMonth, withYear) {
	var text = "";
	text += XKalendar.getWeekDayName(date.getDay());
	text += ", ";
	text += date.getDate();
	text += "th ";
	if(withMonth) {
		text += "of "
		text += XKalendar.getMonthName(date.getMonth());
		if(withYear) {
			text += ", ";
			text += date.getFullYear();
		}
	}
	
	return text;
}

XKalendar.getWeekName = function(date1, date2, withMonth, withYear) {
	var text = "";
	var text = XKalendar.getDateName(date1, true, true);
	text += " - " + XKalendar.getDateName(date2, true, true);
	return text;
}

XKalendar.createRowsHeaders = function(calendarModel) {
	var numberOfRows = 24 * 2;
	var showText = true;
	for(var i=0; i<numberOfRows; i++) {
		var rowHeaderModel = calendarModel.createRowHeader();
		
		if(showText) {
			var text = (i/2) + ":00";
			if(i<10) {
				text = "0" + text;
			}
			rowHeaderModel.setText(text);
			showText = false;
		} else {
			rowHeaderModel.setText("&nbsp;");
			showText = true;
		}
	}
}

XKalendar.createColumn = function(calendarModel, date) {
	var columnModel = calendarModel.createColumn();
	
	var columnDate = new Date(date);
	columnModel.setDate(columnDate);
	
	var numberOfRows = 24 * 2;
	
	for(var i=0; i<numberOfRows; i++) {
		var cellModel = columnModel.createCell();
		//cellModel.getHeader().setText("test")
	}
}

XKalendar.createModelForDay = function(date) {
	var calendarModel = new XKalendarModel();
	calendarModel.configureForDayMode(date);
	return calendarModel;
}

XKalendar.createModelForWeek = function(date1, date2) {
	var calendarModel = new XKalendarModel();
	calendarModel.configureForWeekMode(date1, date2);
	return calendarModel;
}

XKalendar.createModelForThisWeek = function() {
	var calendarModel = new XKalendarModel();
	calendarModel.switchToWeekMode();
	return calendarModel;
}

XKalendar.createEntriesViews = function(calendarModel, calendarView) {
	for(i=0; i<calendarModel.getNumberOfEntries(); i++) {
		var entryModel = calendarModel.getEntry(i);
		var entryView = XKalendar.createEntryView(entryModel);
		calendarView.addView(entryView);	
	}
}

XKalendar.createEntryView = function(entryModel) {
	var entryView = new WindView();
	entryView.setCssClass("XKalendarEntry");
	entryView.setResizeable(true);

	entryView.setX(229);
	entryView.setY(100);

	entryView.setModel(entryModel);
	entryModel.setView(entryView);

	var headerView = new WindView();
	headerView.setCssClass("XKalendarEntryHeader");
	headerView.setText(entryModel.getTitle());
	entryView.addView(headerView);

	entryView.getHeader = function() {
		return headerView;
	}

	return entryView;
}

/* Dynamic entity resizing */

XKalendarEntryExtending = new Object();

XKalendar.startExtendingEntry = function(entryModel) {
	if(XKalendar.extendingEntry()) {
		XKalendar.stopExtendingEntry();
	}
	XKalendarEntryExtending.entryModel = entryModel;
}

XKalendar.extendingEntry = function() {
	return XKalendar.getExtendingEntry() != null;
}

XKalendar.getExtendingEntry = function() {
	return XKalendarEntryExtending.entryModel;
}

XKalendar.stopExtendingEntry = function() {
	XKalendarEntryExtending.entryModel = null;
}



/* Dynamic entity creation */

XKalendarEntryCreation = new Object();

XKalendar.startEntityCreation = function(entryModel) {
	XKalendarEntryCreation.entryModel = entryModel;
}

XKalendar.getEntryCreation = function() {
	return XKalendarEntryCreation.entryModel;
}

XKalendar.stopEntityCreation = function() {
	XKalendarEntryCreation.entryModel = null;
}

XKalendar.entityCreationMouseDoubleClick = function(cellModel, cellView, calendarModel, calendarView) {
	var entryModel = calendarModel.createEntry();
	
	var title = "Calendar Entry B";
	var startDate = new Date();
	var finishDate = new Date();
	
	entryModel.setTitle(title);
	finishDate.setMinutes(finishDate.getMinutes() + 75);
	entryModel.setStartDate(startDate);
	entryModel.setFinishDate(startDate);
	
	var entryView = XKalendar.createEntryView(entryModel);
	calendarView.addView(entryView);
	
	var top = WindView.getTdTop(cellView.getHtmlElement());
	var left = WindView.getTdLeft(cellView.getHtmlElement());
	entryView.setX(left);
	entryView.setY(top);
	entryView.setWidth(cellView.getHtmlElement().offsetWidth);
	entryView.hide();
	
	XKalendar.startEntityCreation(entryModel);
}

XKalendar.entityCreationMouseMove = function(e) {
	var entityModel = XKalendar.getEntityCreation();
	if(entityModel!=null) {
		
		var mouseY = Event.pointerY(e);
	}
} 

XKalendar.entityCreationMouseUp = function(e) {
	var entityModel = XKalendar.getEntityCreation();
	if(entityModel!=null) {
		XKalendar.stopEntityCreation(entryModel);
	}
} 

